requireJS(['constant', 'site', 'handlebars','public'], function (Constant, site, Handlebars ,Public) {

    //注册handlebars
    Constant.initHandlebars(Handlebars);
    //表单验证同时阻止程序继续运行

    var url = {
        list: "manager/queryAllWorkFlow.do",
        updateWorkflow: "manager/updateWorkFlow.do",
        insertWorkflow: "manager/insertWorkFlow.do",
        delWorkflow: "manager/delWorkFlow.do",
        selectPageLogList: "manager/selectPageLogList.do",
    }

    var product_id;

    //全选功能
    $("input[name='ch_id_all']").on('click', function () {
        var _checked = $("input[name='ch_id']")
        if ($(this).prop('checked')) {
            _checked.prop('checked', true)
        }
        else {
            _checked.prop('checked', false)
        }
    })

    $(".datepicker").datepicker({
        format: 'yyyy-mm-dd',
        autoclose: true
    })


    function validate(formObj) {
        for (i in formObj) {
            if (formObj[i] == null || formObj[i] == "") {
                alert("请填写完整")
                return false
            } else {
                return true
            }
        }
    }

    function operation() {
        //操作栏
        $(".operations").on('change', function () {
            id = $(this).attr('flow_id');
            product_id = $(this).attr('product_id');
            var selected = $(this).find("option:selected");
            if (selected.val() == 'editingProcess') {
                $('#product_edit_pop').toggle().attr('flow_id', id);

                //编辑，将已保存的产品信息插入
                var v = workflows[id];
                $("#new_workflow").val(v.workflow_name);
                $("#new_describe").val(v.workflow_describe);
                // $('#new_department option[value=' + v.department_id + ']').attr("selected", false).attr('selected', true);
                // $('#new_contract option[value=' + v.controct_id + ']').attr("selected", false).attr('selected', true);
            }
            if (selected.val() == 'setProcess') {
                var workflow_id = $(this).attr('workflow_id')
                var workflow_name = $(this).attr('workflow_name')
                if(product_id){
                    pjaxGo("process/process.html?productId=" + product_id + "&workflowId=" + workflow_id + "&productName=" + workflow_name)
                }else{
                    pjaxGo("process/process.html?productName=" + workflow_name)
                }
            }
            if (selected.val() == 'delProcess') {
                $(".delete_winBox").show();
            }
        })
    }

    //确认删除
    $("#sureDelete").on('click',function(){
        var data = {
            ids: product_id,
        }
        site.send({
            url: url.delWorkflow,
            data: data,
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == 1) {
                    $(".getLoan_winBox").toggle();
                    Constant.fn.Message('T',data.msg)
                    pjaxReload();
                }else{
                    Constant.fn.Message('F',data.msg)
                }
            }
        })
        $(this).find("option:first").prop("selected", false).prop("selected", true);
    })


    //操作记录
    /*function record() {
        $(".operations_record").on('click', function () {
            var workflow_id = $(this).attr('flow_id')
            var frontData = {
                Table: 't_workflow',
                tableId: workflow_id,
                pageNum: 1
            }
            /!*$('#record_pop ').toggle();*!/
            site.send({
                url: url.selectPageLogList,
                data: frontData,
                callback: function (data) {
                    data = JSON.parse(data);
                    if (data.type == 1) {
                        var source = $("#record_list_tpl").html();
                        var template = Handlebars.compile(source);
                        var list = data.content;
                        var html = template(list);
                        $('#record_list').html(html);
                        //分页
                        $('.record-page-container').createPager({
                            current: frontData.pageNum,
                            total: data.content.total,
                            pageSize: data.content.pageSize,
                            needInput: false,
                            backFn: function (current) {
                                pageNum = current;
                                queryData();
                            }
                        })
                    }
                }
            })
        })
    }*/


    //新建对象，存入产品列表数据
    var workflows = {};

    //默认查询第一页
    var pageNum = 1;
    //查询产品
    var queryData = function () {
        //查询产品所需字段
        var frontData = {
            pageNum: pageNum
        };
        if($("#start_time").val()){
            frontData.start_time = $("#start_time").val();
        }
        if($("#end_time").val()){
            frontData.end_time = $("#end_time").val();
        }
        if($('#product_name_key option:selected').val()){
            frontData.type = $('#product_name_key option:selected').val()
        }
        if($('#product_name').val()){
            frontData.keyword = $('#product_name').val()
        }
        site.send({
            url: url.list,
            //async:false
            data: frontData,
            callback: function (data) {
                data = JSON.parse(data);
                if(data.type == '1'){


                $('.result').html(data.content.total)
                $('.product-page-container').createPager({
                    current: frontData.pageNum,
                    total: data.content.total,
                    pageSize: data.content.pageSize,
                    needInput: false,
                    backFn: function (current) {
                        pageNum = current;
                        queryData();
                    }
                })
                var source = $("#workflow_list_tpl").html();
                var template = Handlebars.compile(source);

                var menuIndex = getAurlparam('h') || 0;
                var thirdMenusIndex = getAurlparam('t') || 0;
                var leftIndex = getAurlparam('i') || 0;

                var storageContent = JSON.parse(localStorage.getItem('content'));
                var obj = {
                    content: data.content.list,
                    fourthMenus: storageContent.menus[menuIndex].children[leftIndex].children[thirdMenusIndex].children || {}
                }
                var html = template(obj);

                var list = data.content.list;
                for (var i = 0; i < list.length; i++) {
                    var v = list[i];
                    workflows[v.id] = v;
                }
                //注册索引+1的helper
                var handleHelper = Handlebars.registerHelper("addOne", function (index) {
                    //返回+1之后的结果
                    return index + 1;
                });

                $('#workflow_list').html(html);

                operation();

                Public.record('t_workflow')
                //record();

                }
                else{
                    Constant.fn.Message("F",data.msg)
                }
            }
        })
    }
    //进入时搜索
    queryData();

    //搜索
    $("#sousuo").on('click', function () {
        queryData();
    });

    $("#product_edit_show").on('click', function (e) {
        e.stopPropagation();
        $('#product_edit_pop').toggle();
        $("#new_workflow").val("");
        $("#new_describe").val("");
    })

    /*$("#record_show").on('click', function () {
        $('#record_pop').toggle();
    })*/

    //新增产品与修改产品整合
    function addAndRevise(url, i) {
        var data = {
            workflow_name: $("#new_workflow").val(),
            workflow_describe: $("#new_describe").val()
            //id: $("#new_describe").val()
        };
        if (i.parents('#product_edit_pop')) {
            data.id = i.parents('#product_edit_pop').attr('flow_id')
        }
        if (validate(data)) {
            site.send({
                url: url,
                data: data,
                callback: function (data) {
                    data = JSON.parse(data)
                    if (data.type == 1) {
                        $("#new_workflow").val("");
                        $("#new_describe").val("");
                        Constant.fn.Message('T',data.msg)
                        pjaxReload();
                        $('#product_edit_pop').toggle();
                    }
                    else {
                        Constant.fn.Message('F',data.msg)
                    }
                },
                callError: function () {
                    Constant.fn.Message('I',"请求数据错误")
                }
            })

        }
    }

    //修改流程信息

    $("#workflow_edit").on('click', function () {
        if ($(this).parents('#product_edit_pop').attr('flow_id')) {
            addAndRevise(url.updateWorkflow, $(this))
        }
        else {
            addAndRevise(url.insertWorkflow, $(this))
        }
    })


    // 初始化表单
    $("#product_new").on('click', function () {
        $('#product_edit_pop').toggle();
        $("#new_workflow").val("");
        $("#new_describe").val("");
    })


    //批量操作

    $("#bulk_operation").on('change', function () {
        product_id = "";
        var selected = $(this).find("option:selected");
        $(this).find("option:first").prop("selected", false).prop("selected", true);
        $("input[name='ch_id']:checked").each(function (index, item) {
            product_id = product_id + $(item).val() + ","
        })
        product_id = product_id.substring(0, product_id.length - 1)
        if (selected.val() == '删除') {
            //删除产品
            $(".delete_winBox").toggle();
        }
    })
})

